package org.example.controller;

import io.swagger.annotations.*;
import org.example.model.Product;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "商品管理接口")
@RestController
@RequestMapping("/api/products")
public class ProductController {

    private List<Product> products = new ArrayList<>();

    @ApiOperation(value = "创建商品", notes = "创建一个新的商品")
    @ApiResponses({
        @ApiResponse(code = 200, message = "创建成功"),
        @ApiResponse(code = 400, message = "请求参数错误")
    })
    @PostMapping
    public Product createProduct(@ApiParam(value = "商品信息", required = true) @RequestBody Product product) {
        product.setId(System.currentTimeMillis());
        products.add(product);
        return product;
    }

    @ApiOperation(value = "获取商品列表", notes = "获取所有商品信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "category", value = "商品类别", paramType = "query"),
        @ApiImplicitParam(name = "minPrice", value = "最低价格", paramType = "query", dataType = "BigDecimal"),
        @ApiImplicitParam(name = "maxPrice", value = "最高价格", paramType = "query", dataType = "BigDecimal")
    })
    @GetMapping
    public List<Product> getProducts(
            @RequestParam(required = false) String category,
            @RequestParam(required = false) BigDecimal minPrice,
            @RequestParam(required = false) BigDecimal maxPrice) {
        return products.stream()
                .filter(p -> category == null || category.equals(p.getCategory()))
                .filter(p -> minPrice == null || p.getPrice().compareTo(minPrice) >= 0)
                .filter(p -> maxPrice == null || p.getPrice().compareTo(maxPrice) <= 0)
                .collect(Collectors.toList());
    }

    @ApiOperation(value = "获取商品详情", notes = "根据商品ID获取商品详细信息")
    @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/{id}")
    public Product getProductById(@PathVariable Long id) {
        return products.stream()
                .filter(product -> product.getId().equals(id))
                .findFirst()
                .orElse(null);
    }

    @ApiOperation(value = "更新商品", notes = "根据商品ID更新商品信息")
    @PutMapping("/{id}")
    public Product updateProduct(
            @ApiParam(value = "商品ID", required = true) @PathVariable Long id,
            @ApiParam(value = "商品信息", required = true) @RequestBody Product product) {
        products.removeIf(p -> p.getId().equals(id));
        product.setId(id);
        products.add(product);
        return product;
    }

    @ApiOperation(value = "删除商品", notes = "根据商品ID删除商品")
    @DeleteMapping("/{id}")
    public void deleteProduct(@ApiParam(value = "商品ID", required = true) @PathVariable Long id) {
        products.removeIf(product -> product.getId().equals(id));
    }

    @ApiOperation(value = "更新商品库存", notes = "根据商品ID更新库存数量")
    @PatchMapping("/{id}/stock")
    public Product updateStock(
            @ApiParam(value = "商品ID", required = true) @PathVariable Long id,
            @ApiParam(value = "库存数量", required = true) @RequestParam Integer stock) {
        return products.stream()
                .filter(p -> p.getId().equals(id))
                .findFirst()
                .map(p -> {
                    p.setStock(stock);
                    return p;
                })
                .orElse(null);
    }
} 